<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        .el-page-header__content {
            font-size: 18px;
            color: white;
        }
    </style>
</head>
<body>
<div id="app">
    <el-page-header
            style="background-color:#E6A23C;color: white;
                    line-height: 60px"
            @back="goBack" content="新增菜品">
    </el-page-header>
    <el-card style="width: 600px;height: 700px;margin: 0 auto">
        <el-form label-width="80px">
            <el-form-item label="菜品名称">
                <el-input v-model="f.name" type="text"></el-input>
            </el-form-item>
            <el-form-item label="菜品价格">
                <el-input v-model="f.price" type="text"></el-input>
            </el-form-item>
            <el-form-item label="菜品图片">
                <el-upload
                        action="/upload"
                        name="picFile"
                        limit="1"
                        list-type="picture-card"
                        :on-success="handleSuccess"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="insert()">添加菜品</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</div>
</body>
<!--引入axios和vue框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                f:{
                    name:"",
                    price:"",
                    url:""
                }
            }
        },

        methods:{
            goBack(){
                //返回上一页面
                history.back();
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                v.f.url="";
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            insert(){
                if (v.f.url==""){
                    v.$message.error("请选择菜品图片!");
                    return;
                }
                //发出添加菜品请求
                axios.post("/food/insert",v.f).then(function () {
                    location.href="/admin.html";
                })
            },
            handleSuccess(response,file,fileList){
                v.f.url = response;
            }
        }
    })
</script>
</html>